package com.xoa.service.users.impl;

import com.sun.mail.imap.protocol.ID;
import com.xoa.dao.unitmanagement.UnitManageMapper;
import com.xoa.dao.users.OrgManageMapper;
import com.xoa.model.unitmanagement.UnitManage;
import com.xoa.model.users.OrgManage;
import com.xoa.service.sys.SystemInfoService;
import com.xoa.service.users.OrgManageService;
import com.xoa.util.CodeUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.dataSource.ContextHolder;
import org.apache.ibatis.io.Resources;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 创建作者: 韩成冰
 * 创建日期: 2017/5/22 14:12
 * 类介绍: 对Orgmanage操作
 * 构造参数: 无
 **/
@Transactional
@Service
public class OrgManageServiceImpl implements OrgManageService {
    @Resource
    private UnitManageMapper unitManageMapper;
    @Resource
    private OrgManageMapper orgManageMapper;
    @Resource
    private SystemInfoService systemInfoService;

    @Override
    public ToJson<OrgManage> queryId(String locale) {
        ToJson<OrgManage> toJson = new ToJson<OrgManage>(0, "");
        List<OrgManage> org = orgManageMapper.queryId();
        if (org != null && org.size() > 0) {
            /* for (OrgManage orgManage:org){
                 if ("en_US".equals(locale)) {
                     orgManage.setName(orgManage.getEnName());
                 }
                 if ("zh_TW".equals(locale)) {
                     orgManage.setName(orgManage.getFtName());
                 }

             }*/
            //    if ()
            toJson.setFlag(0);
            toJson.setMsg("ok");
            toJson.setObj(org);

        } else {
            toJson.setFlag(1);
            toJson.setMsg("err");

        }

        return toJson;
    }

    /**
     * @param locale
     * @return
     */

    public ToJson<OrgManage> queryId2(String locale, String serverName) {
        ToJson<OrgManage> toJson = new ToJson<OrgManage>(0, "");
        List<OrgManage> org = orgManageMapper.queryId();
        List<OrgManage> orgs = new ArrayList<>();
        toJson.setCode(CodeUtil.getCode(org.get(0).getOid()));
        if (org != null && org.size() > 0) {
            String s = null;
            int a = 0;
            String[] split = serverName.split("\\.");
            s = split[0];
            if (s.length() == 4) {
                a = Integer.parseInt(s);
            }
            if ("oa.8oa.cn".equals(serverName)) {
                for (OrgManage orgManage : org) {
                    if ("1002".equals(orgManage.getOid() + "")) {
                        orgs.add(orgManage);
                    }
                }
            } else if (s.length() == 4) {
                for (OrgManage orgManage : org) {
                    if (a == orgManage.getOid()) {
                        orgs.add(orgManage);
                    }
                }
            } else {
                for (OrgManage orgManage : org) {
                    orgs.add(orgManage);
                }
            }
            toJson.setFlag(0);
            toJson.setMsg("ok");
            toJson.setObj(orgs);

        } else {
            toJson.setFlag(1);
            toJson.setMsg("err");
        }

        return toJson;
    }



    /**
     * 创建作者:  韩成冰
     * 创建日期:  2017/5/22 14:14
     * 函数介绍:  获取所有OrgManage
     * 参数说明:  无
     *
     * @return: List<OrgManage>(OrgManage 的List集合)
     **/
    @Override
    public List<OrgManage> getOrgManage() {

        List<OrgManage> orgManageList = orgManageMapper.getOrgManage();
        return orgManageList;
    }

    /**
     * 创建作者:  韩成冰
     * 创建日期:  2017/5/22 14:15
     * 函数介绍:  根据oid查询某个OrgManage
     * 参数说明:  @param
     *
     * @return: OrgManage
     **/
    @Override
    public OrgManage getOrgManageById(Integer oid) {

        return orgManageMapper.getOrgManageById(oid);
    }

    /**
     * 创建作者:  韩成冰
     * 创建日期:  2017/5/22 14:17
     * 函数介绍:  修改某个OrgManage
     * 参数说明:  @param OrgManage对象
     *
     * @return: void
     **/
    @Override
    public void editOrgManage(OrgManage orgManage, HttpServletRequest request) {

        int a = orgManageMapper.editOrgManage(orgManage);
        if (a > 0) {
            Map<String, String> stringStringMap = systemInfoService.getAuthorization(request);
            if ("true".equals(stringStringMap.get("isAuthStr"))) {//如果授权成功，则去获取授权文件的信息
                int a1 = unitManageMapper.updateUnit("xoa" + orgManage.getOid(), orgManage.getName(), 1);
            }

        }


    }

    /**
     * @创建作者: 韩成冰
     * @创建日期: 2017/5/24 18:50
     * @函数介绍: 添加分公司
     * @参数说明: @param orgManage
     * @return: void
     **/
    @Override
    public ToJson<OrgManage> addOrgManage(OrgManage orgManage, HttpServletRequest request) {
        ToJson<OrgManage> toJson = new ToJson<OrgManage>();
        int i = systemInfoService.getOrgManage(request);
        List<OrgManage> orgManageList = orgManageMapper.getOrgManage();
        if (orgManageList.size() < i) {
            orgManageMapper.addOrgManage(orgManage);
            toJson.setFlag(0);
            toJson.setMsg("ok");
        } else {
            toJson.setFlag(1);
            toJson.setMsg("err");
        }
        return toJson;
    }

    @Override
    public BaseWrapper checkOrgManage(HttpServletRequest request) {
        BaseWrapper wrapper = new BaseWrapper();
        wrapper.setFlag(false);
        wrapper.setCode("-1");
        wrapper.setMsg("默认调用失败");
        int i = systemInfoService.getOrgManage(request);
        List<OrgManage> orgManageList = orgManageMapper.getOrgManage();
        if (orgManageList == null) {
            wrapper.setFlag(true);
            wrapper.setCode("-11111");
            wrapper.setMsg("没有企业信息");
            return wrapper;
        }
        if (orgManageList.size() >= i) {
            wrapper.setFlag(false);
            wrapper.setCode("8001");
            wrapper.setMsg("企业数达到注册上线，如需增加请联系厂商。");
            return wrapper;
        } else {
            wrapper.setFlag(true);
            wrapper.setCode("8002");
            wrapper.setMsg("可以新增企业。");
            return wrapper;
        }

    }

    @Override
    public ToJson<OrgManage> selOrgManageIsOrg() {
        ToJson<OrgManage> json = new ToJson<OrgManage>();
        List<OrgManage> orgManages = orgManageMapper.selOrgManageIsOrg();
        json.setObj(orgManages);
        json.setFlag(0);
        return json;
    }

    @Override
    public ToJson<OrgManage> selFirstOrg() {
        ToJson<OrgManage> json = new ToJson<OrgManage>();
        OrgManage orgManage = orgManageMapper.selFirstOrg();
        json.setObject(orgManage);
        json.setFlag(0);
        return json;
    }

}
